package cn.dwong.msgGateway.service;

import cn.dwong.msgGateway.config.ApplicationContextProvider;
import cn.dwong.msgGateway.entity.TitleContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.Callable;

/**
 * @author 73202
 * @date 2022/3/10 17:49
 */
public class SendCallable implements Callable<String> {

    private String tels;
    private String qos;
    private String sessionId;
    private TitleContent titleContent;
    private MessageService messageService;
    private RedisOperator redisOperator;

    public SendCallable(String tels,String qos,String sessionId,TitleContent titleContent){
        this.qos = qos;
        this.tels = tels;
        this.sessionId = sessionId;
        this.titleContent = titleContent;
        this.messageService = ApplicationContextProvider.getBean(MessageService.class);
        this.redisOperator = ApplicationContextProvider.getBean(RedisOperator.class);
    }


    @Override
    public String call() throws Exception {
        /**
         * 手机号码限流：可以使用redis的sex指令+设置过期时间实现
         */
        while(!redisOperator.setNx(tels,sessionId,1500)){
        }
        //调用发送短信的接口
        return messageService.sendMsg(qos,tels,titleContent);
    }
}
